<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class Profile_model extends CI_Model {

    /**
     * @var unknown_type
     */
    var $id_tab = 0;
    /**
     * @var unknown_type
     */
    var $name_vi = null;
    /**
     * @var unknown_type
     */
    var $name_en = null;

    function __construct() {
        parent::__construct();
    }

    function Profile_model() {
        parent::Model();
    }

    function insert() {
        $sql = "INSERT INTO gtb_profile (vi_name, en_name)
				VALUES(" .
                $this->db->escape($this->vi_name) . ", " .
                $this->db->escape($this->en_name) .
                ")";

        $this->db->query($sql);
        if ($this->db->affected_rows())
            return $this->db->insert_id();
        return 0;
    }

    function update($profile) {
        $sql = "UPDATE gtb_profile
				SET " .
                "vi_name = " . $this->db->escape($profile->vi_name) . "," .
                "en_name = " . $this->db->escape($profile->en_name) .
                "WHERE id_profile = " . intval($profile->id_profile);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function delete($id) {
        if (!is_array($id))
            $id = array($id);
        $ids = trim(implode(',', $id));
        if ($ids == '')
            return;

        echo $sql = "DELETE FROM gtb_access WHERE id_profile IN (" . $ids . ")";
        $this->db->query($sql);

        echo $sql = "DELETE FROM gtb_profile WHERE id_profile IN (" . $ids . ")";
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function getAll($offset = 0, $limit=0) {
        $result = null;

        if ($limit)
            $sql = 'SELECT *
				FROM gtb_profile
				LIMIT ' . intval($offset) . ', ' . intval($limit);
        else
            $sql = 'SELECT *
				FROM gtb_profile';

        $query = $this->db->query($sql);

        $result = $query->result_array();

        return $result;
    }

    function getAllCount() {
        $sql = 'SELECT COUNT(*) as num_record
				FROM gtb_profile';

        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->num_record;
        }

        return null;
    }

    function getAllByAccess($id_parent, $id_profile, $default_tab = '') {
        $result = null;
        /* $sql = 'SELECT t.id_tab id, a.id_profile, t.class_name, t.vi_name, t.en_name, t2.vi_name vi_parent, t2.en_name en_parent, t2.class_name class_parent, t.icon
          FROM gtb_tab t
          INNER JOIN gtb_access a ON a.id_tab = t.id_tab
          INNER JOIN gtb_tab t2 ON t.id_tab = t2.id_tab
          WHERE t.id_parent_tab = '. intval($id_parent) .
          ' AND a.id_profile = '.intval($id_profile).'
          ORDER BY t.position';
         */
        $sql = 'SELECT t.id_tab id, a.id_profile, t.class_name, t.vi_name, t.en_name, t2.vi_name vi_parent, t2.en_name en_parent, t2.class_name class_parent, t.icon, t3.class_name sub_default
				FROM gtb_tab t
				INNER JOIN gtb_access a ON a.id_tab = t.id_tab
			    INNER JOIN gtb_tab t2 ON t.id_tab = t2.id_tab
			    LEFT JOIN gtb_tab t3 ON (t.id_tab = t3.id_parent_tab AND t3.class_name = "' . $default_tab . '")
				WHERE t.id_parent_tab = ' . intval($id_parent) .
                ' AND a.id_profile = ' . intval($id_profile) . '
				ORDER BY t.position';

        $query = $this->db->query($sql);
        $result = $query->result();

        return $result;
    }

    function getById($id_profile) {
        $sql = 'SELECT *
				FROM gtb_profile
				WHERE id_profile = ' . intval($id_profile);
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0];
        }

        return null;
    }

//	
}

// EOF
